/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.wisc.csvParser.plugins;

import javax.swing.*;
import javax.swing.event.*;
import com.wisc.csvParser.plugins.DataRepositoryMysqlVega;
import com.wisc.VegaLibrary.Value;

/**
 *
 * @author  lawinslow
 */
public class JPanelMysqlVega extends javax.swing.JPanel {
    
    private DataRepositoryMysqlVega parent;
    
    /** Creates new form JPanelMysqlVega */
    public JPanelMysqlVega(DataRepositoryMysqlVega repo) {
        initComponents();
        
        parent = repo;
        
        DocumentListener l = new DocumentListener(){
            public void changedUpdate(DocumentEvent e){
                settingsChanged();
            }
            public void removeUpdate(DocumentEvent e){
            }
            public void insertUpdate(DocumentEvent e){
                settingsChanged();
            }
        };
        
        dbPathTB.getDocument().addDocumentListener(l);
        dbNameTB.getDocument().addDocumentListener(l);
        dbUserTB.getDocument().addDocumentListener(l);
        passwordTB.getDocument().addDocumentListener(l);
        passwordTB2.getDocument().addDocumentListener(l);
        
        parent.addEventListener(new DataRepositoryMysqlVega.MysqlRepositoryListener(){
            @Override
            public void eventRaised(DataRepositoryMysqlVega.events e){
                newEvent(e);
            }
        });

        updateDisplay();
        
    }
    
    public void newEvent(DataRepositoryMysqlVega.events e){
        

        if(e==DataRepositoryMysqlVega.events.newValue){
            newCountLabel.setText("New Count: "+ 
                    Long.toString(Value.getNewValueCount()));
            duplicateCountLabel.setText("Duplicate Count: "+
                    Long.toString(Value.getDuplicateValueCount()));
        }else if(e==DataRepositoryMysqlVega.events.started){
            startStopStateChanged(true);
        }else if(e==DataRepositoryMysqlVega.events.stopped){
            startStopStateChanged(false);
        }else if(e==DataRepositoryMysqlVega.events.configured){
            updateDisplay();
        }
                
                
    }
    
    private void startStopStateChanged(boolean startedState){
        startedCheckBox.setState(startedState);
        dbPathTB.setEnabled(!startedState);
        dbNameTB.setEnabled(!startedState);
        dbUserTB.setEnabled(!startedState);
        passwordTB.setEnabled(!startedState);
        passwordTB2.setEnabled(!startedState);
        testButton.setEnabled(!startedState);
        
        //don't re-enable these, they will be re-enabled by the
        // text change event when they are needed
        if(startedState){
            revertButton.setEnabled(!startedState);
            saveButton.setEnabled(!startedState);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dbPathTB = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        dbNameTB = new javax.swing.JTextField();
        dbUserTB = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        revertButton = new javax.swing.JButton();
        testButton = new javax.swing.JButton();
        passwordTB = new javax.swing.JPasswordField();
        passwordTB2 = new javax.swing.JPasswordField();
        startedCheckBox = new java.awt.Checkbox();
        newCountLabel = new javax.swing.JLabel();
        duplicateCountLabel = new javax.swing.JLabel();

        jLabel1.setText("MySql Final Repository");

        jLabel2.setText("DB Host:");

        dbPathTB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbPathTBActionPerformed(evt);
            }
        });
        dbPathTB.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dbPathTBPropertyChange(evt);
            }
        });

        jLabel3.setText("DB User:");

        jLabel4.setText("DB Pass:");

        jLabel5.setText("DB Name:");

        dbNameTB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbNameTBActionPerformed(evt);
            }
        });

        dbUserTB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbUserTBActionPerformed(evt);
            }
        });

        jLabel6.setText("Confirm Pass:");

        saveButton.setText("Save");
        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        revertButton.setText("Revert");
        revertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertButtonActionPerformed(evt);
            }
        });

        testButton.setText("Test");
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });

        passwordTB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordTBActionPerformed(evt);
            }
        });

        startedCheckBox.setEnabled(false);
        startedCheckBox.setLabel("Started?");

        newCountLabel.setText("New Count: 0");

        duplicateCountLabel.setText("Duplicate Count: 0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 200, Short.MAX_VALUE)
                                .addComponent(startedCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel5)
                                            .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel2)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(dbNameTB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(dbUserTB, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(dbPathTB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(revertButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(saveButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(testButton))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(passwordTB, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(26, 26, 26)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(passwordTB2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(newCountLabel)
                            .addComponent(duplicateCountLabel))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(dbPathTB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(dbNameTB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(dbUserTB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(startedCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(passwordTB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordTB2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(newCountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(duplicateCountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(revertButton)
                    .addComponent(testButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed
        updateDisplay();
        saveButton.setEnabled(false);
}//GEN-LAST:event_revertButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if(String.valueOf(passwordTB.getPassword()).compareTo(
                String.valueOf(passwordTB2.getPassword()))!=0){
            JOptionPane.showMessageDialog(
                     (JFrame)SwingUtilities.getAncestorOfClass(JFrame.class,this), 
                     "Passwords not identical. Re-enter.");
            return;
        }
        
        parent.setDBName(dbNameTB.getText());
        parent.setDBPassword(String.valueOf(passwordTB.getPassword()));
        parent.setDBServerPath(dbPathTB.getText());
        parent.setDBUser(dbUserTB.getText());
        saveButton.setEnabled(false);
        revertButton.setEnabled(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void dbPathTBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbPathTBActionPerformed

    }//GEN-LAST:event_dbPathTBActionPerformed

    private void dbNameTBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbNameTBActionPerformed

    }//GEN-LAST:event_dbNameTBActionPerformed

    private void dbUserTBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbUserTBActionPerformed

    }//GEN-LAST:event_dbUserTBActionPerformed

    private void passwordTBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordTBActionPerformed

    }//GEN-LAST:event_passwordTBActionPerformed

    private void dbPathTBPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dbPathTBPropertyChange

    }//GEN-LAST:event_dbPathTBPropertyChange

    private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
        if(!parent.Start()){
            JOptionPane.showMessageDialog(
                (JFrame)SwingUtilities.getAncestorOfClass(JFrame.class,this),
                "Cannot connect using supplied settings.");
        }else{
            parent.Stop();
            JOptionPane.showMessageDialog(
                (JFrame)SwingUtilities.getAncestorOfClass(JFrame.class,this),
                "Connection test successful.");
        }
}//GEN-LAST:event_testButtonActionPerformed
    
    private void settingsChanged(){
        saveButton.setEnabled(true);
        revertButton.setEnabled(true);
    }
    public void updateDisplay(){
        dbUserTB.setText(parent.getDBUser());
        dbPathTB.setText(parent.getDBServerPath());
        dbNameTB.setText(parent.getDBName());
        passwordTB.setText(parent.getDBPass());
        passwordTB2.setText(parent.getDBPass());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField dbNameTB;
    private javax.swing.JTextField dbPathTB;
    private javax.swing.JTextField dbUserTB;
    private javax.swing.JLabel duplicateCountLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel newCountLabel;
    private javax.swing.JPasswordField passwordTB;
    private javax.swing.JPasswordField passwordTB2;
    private javax.swing.JButton revertButton;
    private javax.swing.JButton saveButton;
    private java.awt.Checkbox startedCheckBox;
    private javax.swing.JButton testButton;
    // End of variables declaration//GEN-END:variables
    
}
